package com.example.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ruiji.dto.DishDto;
import com.example.ruiji.pojo.Category;
import com.example.ruiji.pojo.Dish;
import com.example.ruiji.pojo.DishFlavor;
import com.example.ruiji.pojo.R;
import com.example.ruiji.service.CategoryService;
import com.example.ruiji.service.DishFlavorService;
import com.example.ruiji.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController //该类控制器 +返回值 自动转json
@RequestMapping("/dish")
public class DishController {
@Autowired
private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto)

    {
        dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }
    //菜品展示
  @GetMapping("/page")
    public  R<Page> page(int page, int pageSize, String name)
  {


      R<Page> ret= dishService.pagea(page,pageSize,name);
      return ret;
  }
    //菜品回显
    @GetMapping("/{id}")
    public  R<DishDto> show( @PathVariable Long id)
    {
        DishDto show = dishService.show(id);
return  R.success(show);
    }

    //保存更新
    @PutMapping
    public  R<String> savei(@RequestBody DishDto dishDto)
    {
        //更新菜品
        dishService.savei(dishDto);

        return R.success("成功");
    }

    //根据id显示菜品信息  这个Id是菜品分类的id 套餐分类的id

    @GetMapping("/list")
    public R<List<DishDto>> lista(Dish  dish)
    {
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.eq(Dish::getStatus,1);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> lista = dishService.list(queryWrapper);
        //根据id和启售 查询表
        List<DishDto> dishDtos=lista.stream().map(
                item->{
 //将数据给dishdto 同时补全口味
                    DishDto dishDto=new DishDto();
                    //拷贝
                    BeanUtils.copyProperties(item,dishDto);
                    Category category = categoryService.getById(item.getCategoryId());

                   LambdaQueryWrapper<DishFlavor> wrapper=new LambdaQueryWrapper<>();

                   wrapper.eq(DishFlavor::getDishId,item.getId());
                    dishDto.setFlavors(dishFlavorService.list(wrapper));
                   return  dishDto;


                }).collect(Collectors.toList());


        return  R.success(dishDtos);

    }


    @DeleteMapping
    public R<String> delet(@RequestParam List<Long> ids)
    {

        dishService.pldelet(ids);
        return R.success("删除成功");
    }
    //后续功能  批量删除 批量启售 批量停售 +权限控制
    //批量停启售
    @PostMapping("/status/{id}")
    public R<String> up(@RequestParam List<Long> ids,@PathVariable("id") int id)
    {
       //进行更新即可
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        Dish dish=new Dish();
        dish.setStatus(id);
        dishService.update(dish,queryWrapper);
        return R.success("停售成功");
    }
}
